<template>
  <div :class="{'show':show}" class="header-search" style="margin-left: 50px">
    <svg-icon class-name="search-icon" icon-class="headSearch" @click.stop="click" @hover.stop="hover" />
    <el-select
      ref="headerSearchSelect"
      v-model="search"
      :remote-method="querySearch"
      filterable
      default-first-option
      remote
      placeholder="请输入基金名称或代码"
      class="header-search-select"
      @change="change"
    >
      <el-option
        v-for="item in options"
        :key="item.value"
        :value="item.value"
        :label="item.label"
      >
        <span style="float: left;position:relative;display: block" v-html="item._highlightResult" />
        <!--        <span style="float: left;display: block;position: absolute;margin-top: 20px;color: #8492a6;font-size: 13px;" v-html="item._highlightResult" />-->
      </el-option>
      <el-option v-if="options.length>0" key="algoliasearch" value="" label="">
        <a target="_blank" href="https://www.algolia.com/docsearch" class="algolia-search-link"><img src="" alt="algolia-logo" class="algolia-search-logo"></a>
      </el-option>
    </el-select>
  </div>
</template>

<script>

export default {
  name: 'HeaderSearch',
  data() {
    return {
      search: '',
      options: [],
      index: [],
      show: false,
      fuse: undefined
    }
  },
  computed: {
  },
  watch: {
    show(value) {
      if (value) {
        document.body.addEventListener('click', this.close)
      } else {
        document.body.removeEventListener('click', this.close)
      }
    }
  },
  mounted() {
    this.index = this.initAlgoliaIndex()
  },
  methods: {
    hover() {
      this.show = true
    },
    click() {
      this.show = !this.show
      if (this.show) {
        this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.focus()
      }
    },
    close() {
      this.$refs.headerSearchSelect && this.$refs.headerSearchSelect.blur()
      this.options = []
      this.show = false
    },
    change(val) {
      if (val === '') {
        // 如果是空不处理
      } else {
        // 否则直接跳转路由页面
        this.$router.push(`/detail/${val}`)
      }
      this.search = ''
      this.options = []
      this.$nextTick(() => {
        this.show = false
      })
    },
    // 初始化algoliaindex
    initAlgoliaIndex() {
      const algoliaSearch = require('algoliasearch')
      const client = algoliaSearch('GUL8ECLVZO', '8ee2834241b8fce63c3be305eb9015ad')
      return client.initIndex('fundCode')
    },

    generateOption(hits) {
      const res = hits.map(hit => {
        return { value: hit.codeNumber, label: `(${hit.codeNumber})${hit.codeName}`, _highlightResult: hit._highlightResult.codeNumber.value + ' ' + hit._highlightResult.codeName.value }
      })
      return res
    },
    querySearch(query) {
      if (query !== '') {
        this.options = this.index.search(query)
          .then(({ hits }) => {
            console.log(hits)
            this.options = this.generateOption(hits)
          })
          .catch(err => {
            console.log(err)
            this.options = []
          })
      } else {
        this.options = []
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.header-search {
  font-size: 0 !important;

  .search-icon {
    cursor: pointer;
    font-size: 24px;
    vertical-align: middle;
  }

  .header-search-select {
    font-size: 18px;
    transition: width 0.2s;
    width: 0;
    overflow: hidden;
    background: transparent;
    border-radius: 0;
    display: inline-block;
    vertical-align: middle;

   ::v-deep .el-input__inner {
      border-radius: 0;
      border: 0;
      padding-left: 0;
      padding-right: 0;
      box-shadow: none !important;
      border-bottom: 1px solid #d9d9d9;
      vertical-align: middle;
    }
  }

  &.show {
    .header-search-select {
      width: 210px;
      margin-left: 10px;
    }
  }
}

::v-deep em{
  color: #f73131;
  //text-decoration: underline;
  font-style: normal;
}

.algolia-search-link {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  background-color: #e4e7ed;
  //border-bottom-left-radius: 4px;
  //border-bottom-right-radius: 4px;
  box-sizing: border-box;
  text-align: center;
  img {
    display: inline-block;
    height: 14px;
  }
}
a {
  color: #409eff;
  text-decoration: none;
}
</style>
